<?php

class database {
	protected $records;

	function __construct() {
		include_once("../../../api/database.php");
		$this->records = $config;
	}

	function getById($id) {
		return (isset($this->records[$id])) ? $this->records[$id] : false;
	}

	function getList() {
		return( @array_values($this->records) );
	}

	function getCount() {
		return( count($this->records) );
	}

	function deleteDatabase($id) {

		if(isset($this->records[$id])){

			unset($this->records[$id]);
			$results = $this->createConfig();
			$results = "<?php\r\n" . $results . "?>";
			$res = file_put_contents('../../../api/database.php', $results);
			return ($res);
		} else {
    		return (false);
    	}

	}
	function addDatabase($dbConfig) {
		//if (!isset($dbConfig["id"])) return(false);
		$id = uniqid();
		$this->records[$id]["id"] = $id;
		$this->records[$id]["name"] = $dbConfig["name"];
		$this->records[$id]["dbType"] = $dbConfig["dbType"];
		$this->records[$id]["server"] = $dbConfig["server"];
		$this->records[$id]["port"] = $dbConfig["port"];
		$this->records[$id]["userName"] = $dbConfig["userName"];
		$this->records[$id]["pass"] = $dbConfig["pass"];
		$this->records[$id]["title"] = $dbConfig["title"];

		$data = array();
		foreach ($this->records as $test) {
			$data[] = $test;
		}
		$results = $this->createConfig();
		$results = "<?php\r\n" . $results . "?>";
		$res = file_put_contents('../../../api/database.php', $results);
		return($data);
	}

	function updateDatabase($dbConfig) {
		//$this->records[$dbConfig[$id]]["id"] = $dbConfig["id"];
		$this->records[$dbConfig["id"]]["name"] = $dbConfig["name"];
		$this->records[$dbConfig["id"]]["dbType"] = $dbConfig["dbType"];
		$this->records[$dbConfig["id"]]["server"] = $dbConfig["server"];
		$this->records[$dbConfig["id"]]["port"] = $dbConfig["port"];
		$this->records[$dbConfig["id"]]["userName"] = $dbConfig["userName"];
		$this->records[$dbConfig["id"]]["pass"] = $dbConfig["pass"];
		$this->records[$dbConfig["id"]]["title"] = $dbConfig["title"];

		$results = $this->createConfig();
		$results = "<?php\r\n" . $results . "?>";
		$res = file_put_contents('../../../api/database.php', $results);
		return($res);
	}

	private function createConfig() {
		$output = '';
		if(is_array($this->records) && count($this->records)) {
		foreach($this->records as $name => $value) {
			if(is_array($value)) {
			foreach($value as $name2 => $value2) {
				if(is_array($value2)) {
				foreach($value2 as $name3 => $value3) {
					if(is_array($value3)) {
					foreach($value3 as $name4 => $value4) {
						if(is_array($value4)) {
						foreach($value4 as $name5 => $value5) {
							$output .= sprintf("\$config['%s']['%s']['%s']['%s']['%s'] = '%s';\r\n", $name, $name2, $name3, $name4, $name5,  stripslashes($value5) );
						}
						} else {
							$output .= sprintf("\$config['%s']['%s']['%s']['%s'] = '%s';\r\n", $name, $name2, $name3, $name4, stripslashes($value4) );
						}
					}
					} else {
						$output .= sprintf("\$config['%s']['%s']['%s'] = '%s';\r\n", $name, $name2, $name3, stripslashes($value3));
					}
				}
				} else {
					$output .= sprintf("\$config['%s']['%s'] = '%s';\r\n", $name, $name2, stripslashes($value2) );
				}
			}
			} else {
				$output .= sprintf("\$config['%s'] = '%s';\r\n", $name, stripslashes($value) );
			}
		}
		return($output);
		}
		return false;
	}

}
?>